package com.bank.doman.auth.server;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.bank.common.exception.ParamException;
import com.bank.doman.auth.dao.MenuDao;
import com.bank.doman.auth.dao.RoleGrantDao;
import com.bank.doman.auth.dao.model.Menu;
import com.bank.doman.auth.io.bo.MenuAddBo;
import com.bank.doman.auth.io.bo.MenuEditBo;
import com.bank.doman.auth.io.vo.MenuVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/19 16:52
 */
@Service
public class MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private RoleGrantDao roleGrantDao;


    public MenuVo getById(Long uid) {
        return BeanUtil.toBean(menuDao.selectByPrimaryKey(uid), MenuVo.class);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void create(MenuAddBo bo) {
        Menu menu = menuDao.queryMenuByCodeAndType(bo.getCode(),bo.getType());
        ParamException.isNotNull(menu,"code不能重复");
        Menu entity = BeanUtil.toBean(bo, Menu.class);
        menuDao.insert(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(MenuEditBo bo) {
        Menu menu = menuDao.selectByPrimaryKey(bo.getId());
        ParamException.isNull(menu);
        if(!menu.getCode().equals(bo.getCode())){
            ParamException.isNotNull(menuDao.queryMenuByCodeAndType(bo.getCode(),bo.getType()),"code不能重复");
        }
        menuDao.updateByPrimaryKeySelective(BeanUtil.toBean(bo,Menu.class));
    }

    public void delete(Long id) {
        menuDao.deleteById(id);
    }


    /**
     * 树型菜单
     *
     * @return
     */
    public List<MenuVo> tree() {
        List<MenuVo> menuVos = new ArrayList<>();
        List<Menu> menus = menuDao.selectAll();
        for(Menu menu : menus){
            if(menu.getPid()!=null){
                MenuVo menuVo =  BeanUtil.toBean(menu, MenuVo.class);
                getDeptDictTree(menus,menuVo);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }

    private MenuVo getDeptDictTree(List<Menu> menuVos,MenuVo menuVo) {
        List<MenuVo> baseMenu = new ArrayList<>();
        for(Menu menu : menuVos){
            if(menuVo.getId().equals(menu.getPid())){
                MenuVo menuVo1 =  BeanUtil.toBean(menu, MenuVo.class);
                baseMenu.add(menuVo1);
                getDeptDictTree(menuVos,menuVo1);
            }
        }
        menuVo.setChild(baseMenu);
        if(baseMenu.size()<1){
            return menuVo;
        }
        return menuVo;
    }

    public List<MenuVo> getUserMenus(String userId){
        List<MenuVo> menuVos = new ArrayList<>();
        List<Menu> menus = menuDao.queryUserMenu(userId);
        if(CollectionUtil.isEmpty(menus)){
            return menuVos;
        }
        for(Menu menu : menus){
            if(menu!=null&& menu.getPid()!=null){
                MenuVo menuVo =  BeanUtil.toBean(menu, MenuVo.class);
                getDeptDictTree(menus,menuVo);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }

}
